"""__author__=Hanlin"""
import time

import pymysql
import redis
import pickle

def load_depts_from_db():
    con = pymysql.connect(host='', port=3306,
                          user='root', password='zhl901010',
                          database='hrs', charset='utf8')
    depts = ()
    try:
        with con.cursor() as cursor:
            cursor.execute('select dno, dname, dloc from tb_dept')
            depts = cursor.fetchall()
    except pymysql.MySQLError as err:
        print(err)
    finally:
        con.close()
    return depts

def main():
    client = redis.Redis(host='47.96.83.83', port=6379, password='1qaz2wsx')
    start = time.time()
    data = client.get('depts')
    if data:
        depts = pickle.loads(data)
    else:
        depts = load_depts_from_db()
        client.set('depts',pickle.dumps(depts))
    end = time.time()
    print(depts)
    print('执行时间：',end-start)
if __name__ == '__main__':
    main()
